<?php
	require ("imdbsearch.class.php");
	
	$temp = array();
	exec("defaults read com.tegosaurus.appletvdatamaker",$temp);
	array_shift($temp); array_pop($temp);
	$prefs = array();
	foreach($temp as $line){
		$line = substr($line,0,strlen($line)-1);
		list($key,$value) = explode(" = ",trim($line),2);
		$prefs[$key] = $value;
	}
	if(!isset($prefs["noPartTitle"])){ $prefs["noPartTitle"] = 1; }
	
	
	
	$subdirname = $argv[1];
	$movtypes	= array("mov","avi","mpg","mpeg","divx","ogm","mp4","mkv","m4v","mp4v");	// What are movie file extensions
	$subdir = opendir($subdirname);
		$filesize	= 0;
		$xml		= array();
		$xmlfile	= false;
		$cover		= false;
		$subtitles	= false;
		$listavi	= array();
		$listavism	= array();
		$sample		= "";
		$trailer	= "";
		$movcount	= 0;
		$xmlcount	= 0;
		$jpgcount	= 0;
		$srtcount	= 0;
		$nfofile	= "";

		while(false !== ($file = readdir($subdir))){
			if(substr($file,0,1) == "."){ continue; }				// No hidden files / dirs
			$ext = strtolower(substr($file, strrpos($file, ".")+1));
			if($ext == ""){ continue; }
			if(in_array($ext, $movtypes)){							// It is a movie
				$movcount++;
				if(strpos($file, "samp") > 0){						// It is a sample
					$sample = $file;
				}elseif(strpos($file, "trailer") > 0){				// It is a trailer
					$trailer = $file;
				}else{												// It is a part of the movie
					array_push($listavism, substr($file, 0, strrpos($file, ".")));
					array_push($listavi, $file);
					if($filesize == 0){								// Get file size of first movie, for subtitle downloading...
						$filesize = filesize($subdirname."/".$file);
					}
				}
			}
		}

		if(!file_exists($subdirname."/imdbnr.txt")){
			$h = fopen($subdirname."/imdbnr.txt","w");
			fwrite($h, $argv[2]);
		}

		$movie = new imdb($argv[2]);
		$movieid = $argv[2];

		$movie->setid ($movieid);
		$cols = $movie->colors ();
		$sound = $movie->sound ();
		$country = $movie->country ();

		if(!is_array($cols)){ $cols = array($cols); }
		if(!is_array($akas)){ $akas = array($akas); }
		if(!is_array($country)){ $country = array($country); }
		if(!is_array($sound)){ $sound = array($sound); }

		$xml = '<media type="Movie">
	<title>'.$movie->title().'</title>
	<description><![CDATA[';
		$plot = $movie->plot (); 
		$xml .= preg_replace("/\<i\>.+?Written.*/s","",str_replace("\n","",$plot[0]));
		$xml .= ']]></description>
';
		$certs = $movie->mpaa();
		if(isset($certs["Belgium"])){ $xml .= '	<rating>'.$certs["Belgium"].' (Belgium)</rating>'."\n"; }
		elseif(isset($certs["USA"])){ $xml .= '	<rating>'.$certs["USA"].'</rating>'."\n"; }

		$rating = $movie->rating();
		$xml .= '	<starRating>'.round($rating/2)."</starRating>\n";
		$xml .= '	<imdbRating>'.$rating."</imdbRating>\n";
		$xml .= '	<imdbnr>'.$argv[2]."</imdbnr>\n";
		$xml .= '	<published>'.$movie->year()."-01-01</published>\n";
		$xml .= '	<genres>'."\n";
		$gen = $movie->genres ();
		$setgenres = array();
		$i=0;
		foreach($gen as $genre){
			if(in_array($genre, $setgenres)){ continue; }
			$xml .= '		<genre'.(($i==0)?' primary="true"':'').'>'.$genre.'</genre>'."\n";
			array_push($setgenres, $genre);
			$i++;
		}
		$xml .= "\t</genres>\n";
		$xml .= "\t<cast>\n";
		$cast = $movie->cast();
		foreach($cast as $member){
			$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
		}
		$xml .= "\t</cast>\n";
		
		if(!isset($argv[3]) or ($argv[3] != "chris")){
			$xml .= "\t<producers>\n";
			$cast = $movie->producer();
			foreach($cast as $member){
				$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
			}
			$xml .= "\t</producers>\n";
			$xml .= "\t<directors>\n";
			$cast = $movie->director();
			foreach($cast as $member){
				$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
			}
			$xml .= "\t</directors>\n";
			$xml .= "\t<authors>\n";
			$cast = $movie->writing();
			foreach($cast as $member){
				$xml .= '		<name><![CDATA['.$member["name"].']]></name>'."\n";
			}
			$xml .= "	</authors>\n";
		}
		
		$xml .= "</media>";
		



	// Wegschrijven...
	
	sort($listavi);
	$i = 0;
	foreach($listavi as $avi){
		$i++;
		$ext = strtolower(substr($avi, strrpos($avi, ".")+1));
		if(count($listavi) > 1){
			if($prefs["noPartTitle"] != "1"){
				$txml = str_replace("</title>"," / Part ".$i."</title>", $xml);
			}else{
				$txml = $xml;
			}
		}else{
			$txml = $xml;
		}
		$x = fopen($subdirname."/".str_replace(".".$ext,".xml",$avi),"w");
		fwrite($x, $txml);
	}
	if($sample != ""){
		$ext = strtolower(substr($sample, strrpos($sample, ".")+1));
		$txml = str_replace("</title>"," / Sample</title>", $xml);
		$x = fopen($subdirname."/".str_replace(".".$ext,".xml",$sample),"w");
		fwrite($x, $txml);
	}
	if($trailer != ""){
		$ext = strtolower(substr($trailer, strrpos($trailer, ".")+1));
		$txml = str_replace("</title>"," / Trailer</title>", $xml);
		$x = fopen($subdirname."/".str_replace(".".$ext,".xml",$trailer),"w");
		fwrite($x, $txml);
	}

	$temp	= fopen($subdirname."/movietitle.txt","w");
	fwrite($temp, $movie->title());

	echo '1';
	
?>
